<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 <span class="color_h1">元组</span></h1>

        <p>Python 的元组与列表类似，不同之处在于元组的元素不能修改。</p>
        <p>元组使用小括号，列表使用方括号。</p>
        <p>元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。</p>
        <p>如下实例：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">tup1 </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">(</span><span class="str">'Google'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'Runoob'</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">1997</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">2000</span><span
                class="pun">);</span><span class="pln">
tup2 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">5</span><span class="pln"> </span><span class="pun">);</span><span class="pln">
tup3 </span><span class="pun">=</span><span class="pln"> </span><span class="str">"a"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"b"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"c"</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">"d"</span><span
                class="pun">;</span></pre>
        <p>创建空元组</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">tup1 </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">();</span></pre>
        <p>元组中只包含一个元素时，需要在元素后面添加逗号，否则括号会被当作运算符使用：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> tup1 </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">(</span><span class="lit">50</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> type</span><span class="pun">(</span><span class="pln">tup1</span><span
                class="pun">)</span><span class="pln">     </span><span class="com"># 不加逗号，类型为整型</span><span
                class="pln">
</span><span class="pun">&lt;</span><span class="kwd">class</span><span class="pln"> </span><span
                class="str">'int'</span><span class="pun">&gt;</span><span class="pln">

</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> tup1 </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">(</span><span class="lit">50</span><span
                class="pun">,)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> type</span><span class="pun">(</span><span class="pln">tup1</span><span
                class="pun">)</span><span class="pln">     </span><span class="com"># 加上逗号，类型为元组</span><span
                class="pln">
</span><span class="pun">&lt;</span><span class="kwd">class</span><span class="pln"> </span><span
                class="str">'tuple'</span><span class="pun">&gt;</span></pre>
        <p>元组与字符串类似，下标索引从0开始，可以进行截取，组合等。</p>


        <h2>访问元组</h2>
        <p>元组可以使用下标索引来访问元组中的值，如下实例:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

tup1 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">'Google'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Runoob'</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">1997</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">2000</span><span
                class="pun">)</span><span class="pln">
tup2 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span
                class="pun">)</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"tup1[0]: "</span><span class="pun">,</span><span class="pln"> tup1</span><span class="pun">[</span><span
                class="lit">0</span><span class="pun">])</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">"tup2[1:5]: "</span><span class="pun">,</span><span class="pln"> tup2</span><span
                class="pun">[</span><span class="lit">1</span><span class="pun">:</span><span class="lit">5</span><span
                class="pun">])</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">tup1</span><span class="pun">[</span><span
                class="lit">0</span><span class="pun">]:</span><span class="pln">  </span><span
                class="typ">Google</span><span class="pln">
tup2</span><span class="pun">[</span><span class="lit">1</span><span class="pun">:</span><span class="lit">5</span><span
                class="pun">]:</span><span class="pln">  </span><span class="pun">(</span><span
                class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">5</span><span class="pun">)</span></pre>


        <h2>修改元组</h2>
        <p>元组中的元素值是不允许修改的，但我们可以对元组进行连接组合，如下实例:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

tup1 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span
                class="lit">12</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">34.56</span><span class="pun">);</span><span class="pln">
tup2 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="str">'abc'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'xyz'</span><span
                class="pun">)</span><span class="pln">

</span><span class="com"># 以下修改元组元素操作是非法的。</span><span class="pln">
</span><span class="com"># tup1[0] = 100</span><span class="pln">

</span><span class="com"># 创建一个新的元组</span><span class="pln">
tup3 </span><span class="pun">=</span><span class="pln"> tup1 </span><span class="pun">+</span><span
                class="pln"> tup2</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="pln">tup3</span><span class="pun">)</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">(</span><span class="lit">12</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">34.56</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'abc'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'xyz'</span><span
                class="pun">)</span></pre>

        <h2>删除元组</h2>
        <p>元组中的元素值是不允许删除的，但我们可以使用del语句来删除整个元组，如下实例:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

tup </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">'Google'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Runoob'</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">1997</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">2000</span><span
                class="pun">)</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="pln">tup</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">del</span><span class="pln"> tup</span><span class="pun">;</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"删除后的元组 tup : "</span><span
                class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="pln">tup</span><span class="pun">)</span></pre>
        <p>以上实例元组被删除后，输出变量会有异常信息，输出如下所示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">删除后的元组</span><span
                class="pln"> tup </span><span class="pun">:</span><span class="pln">
</span><span class="typ">Traceback</span><span class="pln"> </span><span class="pun">(</span><span class="pln">most recent call </span><span
                class="kwd">last</span><span class="pun">):</span><span class="pln">
  </span><span class="typ">File</span><span class="pln"> </span><span class="str">"test.py"</span><span
                class="pun">,</span><span class="pln"> line </span><span class="lit">8</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span
                class="str">&lt;module&gt;</span><span class="pln">
    </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span
                class="pln">tup</span><span class="pun">)</span><span class="pln">
</span><span class="typ">NameError</span><span class="pun">:</span><span class="pln"> name </span><span class="str">'tup'</span><span
                class="pln"> </span><span class="kwd">is</span><span class="pln"> </span><span
                class="kwd">not</span><span class="pln"> </span><span class="kwd">defined</span></pre>

        <h2>元组运算符</h2>
        <p>与字符串一样，元组之间可以使用 + 号和 * 号进行运算。这就意味着他们可以组合和复制，运算后会生成一个新的元组。</p>
        <table class="reference">
            <tbody>
            <tr>
                <th style="width:33%">Python 表达式</th>
                <th style="width:33%">结果</th>
                <th style="width:33%"> 描述</th>
            </tr>
            <tr>
                <td>len((1, 2, 3))</td>
                <td>3</td>
                <td>计算元素个数</td>
            </tr>
            <tr>
                <td>(1, 2, 3) + (4, 5, 6)</td>
                <td>(1, 2, 3, 4, 5, 6)</td>
                <td>连接</td>
            </tr>
            <tr>
                <td>('Hi!',) * 4

                </td>
                <td>('Hi!', 'Hi!', 'Hi!', 'Hi!')</td>
                <td>复制</td>
            </tr>
            <tr>
                <td>3 in (1, 2, 3)</td>
                <td>True</td>
                <td>元素是否存在</td>
            </tr>
            <tr>
                <td>for x in (1, 2, 3): print x,</td>
                <td>1 2 3</td>
                <td>迭代</td>
            </tr>
            </tbody>
        </table>

        <h2>元组索引，截取</h2>
        <p>因为元组也是一个序列，所以我们可以访问元组中的指定位置的元素，也可以截取索引中的一段元素，如下所示：</p>
        <p>元组：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">L </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">(</span><span class="str">'Google'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'Taobao'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'Runoob'</span><span
                class="pun">)</span></pre>

        <table class="reference">
            <tbody>
            <tr>
                <th style="width:33%">Python 表达式</th>
                <th style="width:33%">结果</th>
                <th style="width:33%"> 描述</th>
            </tr>
            <tr>
                <td>L[2]</td>
                <td>'Runoob'</td>
                <td>读取第三个元素</td>
            </tr>
            <tr>
                <td>L[-2]</td>
                <td>'Taobao'</td>
                <td>反向读取；读取倒数第二个元素</td>
            </tr>
            <tr>
                <td>L[1:]</td>
                <td>('Taobao', 'Runoob')</td>
                <td>截取元素，从第二个开始后的所有元素。</td>
            </tr>
            </tbody>
        </table>
        <p>运行实例如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> L </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">(</span><span class="str">'Google'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'Taobao'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'Runoob'</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> L</span><span class="pun">[</span><span
                class="lit">2</span><span class="pun">]</span><span class="pln">
</span><span class="str">'Runoob'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> L</span><span class="pun">[-</span><span
                class="lit">2</span><span class="pun">]</span><span class="pln">
</span><span class="str">'Taobao'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> L</span><span class="pun">[</span><span
                class="lit">1</span><span class="pun">:]</span><span class="pln">
</span><span class="pun">(</span><span class="str">'Taobao'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'Runoob'</span><span class="pun">)</span></pre>



        <h2>元组内置函数</h2>
        <p>Python元组包含了以下内置函数</p>
        <table class="reference">
            <tbody>
            <tr>
                <th style="width:5%">序号</th>
                <th style="width:25%">方法及描述</th>
                <th style="width:45%">实例</th>
            </tr>

            <tr>
                <td>1</td>
                <td>len(tuple)<br>计算元组元素个数。</td>
                <td>
<pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
        class="pln"> tuple1 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span
        class="str">'Google'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Runoob'</span><span
        class="pun">,</span><span class="pln"> </span><span class="str">'Taobao'</span><span class="pun">)</span><span
        class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> len</span><span class="pun">(</span><span class="pln">tuple1</span><span
        class="pun">)</span><span class="pln">
</span><span class="lit">3</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span></pre>
                </td>
            </tr>
            <tr>
                <td>2</td>
                <td>max(tuple)<br>返回元组中元素最大值。</td>
                <td>
<pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
        class="pln"> tuple2 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span
        class="str">'5'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'4'</span><span
        class="pun">,</span><span class="pln"> </span><span class="str">'8'</span><span class="pun">)</span><span
        class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> max</span><span class="pun">(</span><span class="pln">tuple2</span><span
        class="pun">)</span><span class="pln">
</span><span class="str">'8'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span></pre>
                </td>

            </tr>
            <tr>
                <td>3</td>
                <td>min(tuple)<br>返回元组中元素最小值。</td>
                <td>
<pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
        class="pln"> tuple2 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span
        class="str">'5'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'4'</span><span
        class="pun">,</span><span class="pln"> </span><span class="str">'8'</span><span class="pun">)</span><span
        class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> min</span><span class="pun">(</span><span class="pln">tuple2</span><span
        class="pun">)</span><span class="pln">
</span><span class="str">'4'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span></pre>
                </td>
            </tr>
            <tr>
                <td>4</td>
                <td>tuple(seq)<br>将列表转换为元组。</td>
                <td>
<pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
        class="pln"> list1</span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span
        class="str">'Google'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Taobao'</span><span
        class="pun">,</span><span class="pln"> </span><span class="str">'Runoob'</span><span class="pun">,</span><span
        class="pln"> </span><span class="str">'Baidu'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> tuple1</span><span class="pun">=</span><span class="pln">tuple</span><span
        class="pun">(</span><span class="pln">list1</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> tuple1
</span><span class="pun">(</span><span class="str">'Google'</span><span class="pun">,</span><span
        class="pln"> </span><span class="str">'Taobao'</span><span class="pun">,</span><span class="pln"> </span><span
        class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
        class="str">'Baidu'</span><span class="pun">)</span></pre>
                </td>
            </tr>
            </tbody>
        </table>

    </div>
</div>
</body>
</html>
